package project.active;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.pojo.Employee;
import model.pojo.Languages;


import project.adapter.Projectlist_adapter_tc;
import project.dao.pr_projectDAO;
import project.model.PrProject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import approval.dao.LangDAO;
import approve.action.LoginActivity;
import approve.action.R;
public class Projectlist_tc extends Activity {
	Bundle language;
	pr_projectDAO pr=new pr_projectDAO();
	ListView projectlist_tc;
	TextView logout,username,report,lbprolist;
	Employee emp=new Employee();
	String lang = "EN",database="admin";
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.projectlist_tc);
		
		language = getIntent().getExtras().getBundle(getLang());
		Intent i=this.getIntent();
		emp=(Employee)i.getSerializableExtra("em");
		
		projectlist_tc =(ListView)findViewById(R.id.projectlist_tc);
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		lbprolist=(TextView)findViewById(R.id.lbprolist);

		logout.setText(language.getString("Logout"));
		username.setText(emp.getUserName());
		lbprolist.setText(language.getString("ac_SUProjectList"));
		logout.setOnClickListener(listenerLogout);
		
		try {
			Projectlist_adapter_tc adapter1=new Projectlist_adapter_tc(this, loadList(emp.getIdemployee()),language.getString("ac_lien_STu"),language.getString("ac_lien_SDen"));
			projectlist_tc.setAdapter(adapter1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		projectlist_tc.setOnItemClickListener(listenerProjectList_tc);
		
		
		
		
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Projectlist_tc.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	
	private OnItemClickListener listenerProjectList_tc=new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view,int position, long id){
			Intent i=new Intent(Projectlist_tc.this,Projectdetail_tab_tc.class);
			PrProject prr=(PrProject)projectlist_tc.getItemAtPosition(position);
			i.putExtras(getIntent().getExtras());
			i.putExtra("lang", lang);
			i.putExtra("project", prr);
			i.putExtra("em", emp);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	public List<PrProject> loadList(String idEmp) throws SQLException{
//		List<PrProject> item=new ArrayList<PrProject>();
//		item=pr.getListproject_tc(getDatabase(),idEmp);
		
		List<PrProject> item = pr.getListproject_tc(getDatabase(),idEmp);
		if(item.size()==0){
			TextView  nodata = new TextView(this);
    		nodata.setText(language.getString("nodata"));
    		setContentView(nodata);
		}
		else{
			return item;
		}
		return item;
	}
	
}
